package org.ddevil.data;


/**
 *
 * @author Rich O'Connell
 */
public abstract class AbstractData implements Data {

	/**
	 * {@inheritDoc}
	 */
	synchronized public void copyDataFrom(Data in) {
		for (Attribute a : in) {
			setDataItem(a.getId(), a.getValue());
		}
	}

	synchronized public void copyDataFrom(Data in, String...attributes) {
		for (String a : attributes) {
			setDataItem(a, in.getValue(a));
		}
	}




	/**
	 * {@inheritDoc}
	 */
	synchronized public void addDataItem(Attribute toAdd) {
		setDataItem(toAdd.getId(), toAdd.getValue());
	}

	/**
	 * {@inheritDoc}
	 */
	public Attribute getDataItem(String id) {
		if (contains(id)) {
			return new Attribute(id, getValue(id));
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (Attribute a : this) {
			sb.append(a.getId()).append("=").append(a.getValue()).append(",");
		}
		return sb.substring(0, sb.lastIndexOf(","));
	}

}
